
Imports std
Imports System.IO

Module main

    Public Class encodingName
        Public Const utf8 As String = "utf8"
        Public Const utf8bom As String = "utf8bom"
        Public Const unicode As String = "unicode"
        Public Const beunicode As String = "beunicode"
        Public Const ascii As String = "ascii"
    End Class

    Sub Main(ByVal args() As String)
        On Error Resume Next
        Dim istream As FileStream = Nothing
        Dim ostream As FileStream = Nothing
        Dim reader As TextReader = Nothing
        Dim writer As TextWriter = Nothing
        Dim input As String = Nothing
        Dim argIndex As Int32 = 0
        Dim i As Int32
        Dim inputEncoding As String = Nothing
        Dim outputEncoding As String = Nothing

        If args.Length < 2 Then
            raiseError("argument count error, encoding [-i <inputEncoding>] [-o <outputEncoding>] input output, " + _
                        "internal encoding support " + encodingName.utf8 + _
                        " " + encodingName.utf8bom + _
                        " " + encodingName.unicode + _
                        " " + encodingName.beunicode + _
                        " " + encodingName.ascii + ", default output is " + encodingName.utf8 + _
                        ", other support based on environment. if input or output is /-, use console.", _
                        errorHandle.errorType.critical)
        Else
            For i = 0 To args.Length - 3 Step 2
                If strsame(args(i), "-i", False) Then
                    inputEncoding = args(i + 1)
                ElseIf strsame(args(i), "-o", False) Then
                    outputEncoding = args(i + 1)
                End If
            Next

            If strsame(args(args.Length() - 2), consoleIOArgument) Then
                reader = Console.In
            Else
                istream = New FileStream(args(args.Length() - 2), FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
                If Not isemptystring(inputEncoding) Then
                    If strsame(inputEncoding, encodingName.utf8bom) Then
                        reader = New StreamReader(istream, encoding.utf8bom)
                    ElseIf strsame(inputEncoding, encodingName.unicode) Then
                        reader = New StreamReader(istream, encoding.unicode)
                    ElseIf strsame(inputEncoding, encodingName.beunicode) Then
                        reader = New StreamReader(istream, encoding.beunicode)
                    ElseIf strsame(inputEncoding, encodingName.ascii) Then
                        reader = New StreamReader(istream, encoding.ascii)
                    ElseIf strsame(inputEncoding, encodingName.utf8) Then
                        reader = New StreamReader(istream, encoding.utf8)
                    Else
                        reader = New StreamReader(istream, System.Text.Encoding.GetEncoding(inputEncoding))
                    End If
                Else
                    reader = New StreamReader(istream)
                End If
            End If

            If strsame(args(args.Length() - 1), consoleIOArgument) Then
                writer = Console.Out
            Else
                ostream = New FileStream(args(args.Length() - 1), FileMode.Create, FileAccess.Write, FileShare.Read)
                If Not isemptystring(outputEncoding) Then
                    If strsame(outputEncoding, encodingName.utf8bom) Then
                        writer = New StreamWriter(ostream, encoding.utf8bom)
                    ElseIf strsame(outputEncoding, encodingName.unicode) Then
                        writer = New StreamWriter(ostream, encoding.unicode)
                    ElseIf strsame(outputEncoding, encodingName.beunicode) Then
                        writer = New StreamWriter(ostream, encoding.beunicode)
                    ElseIf strsame(outputEncoding, encodingName.ascii) Then
                        writer = New StreamWriter(ostream, encoding.ascii)
                    ElseIf strsame(outputEncoding, encodingName.utf8) Then
                        writer = New StreamWriter(ostream, encoding.utf8)
                    Else
                        writer = New StreamWriter(ostream, System.Text.Encoding.GetEncoding(outputEncoding))
                    End If
                Else
                    writer = New StreamWriter(ostream)
                End If
            End If

            assert(Not reader Is Nothing, "file no found, " + args(args.Length() - 2))
            assert(Not writer Is Nothing, "file no found, " + args(args.Length() - 1))

            input = reader.ReadLine()
            While Not input Is Nothing
                writer.WriteLine(input)
                input = reader.ReadLine()
            End While

            closestream(reader)
            closestream(writer)
        End If
    End Sub

End Module
